package com.mapbased.sfw.model;

import org.jboss.netty.handler.codec.http.HttpHeaders;

import com.mapbased.sfw.binding.Link;
import com.mapbased.sfw.store.PathData;
import com.mapbased.sfw.store.PathDataType;
import com.mapbased.sfw.util.MimeMapping;

public class ResBag   extends PathBag {

	public static long ONE_YEAR=365*24*3600*1000L;
	Site site;
	private  String path;
	private byte[] value;
	private long lastModified;
	public ResBag(PathData data){
		this.lastModified=data.lastModified;
		this.path=data.path;
		//this.value=data.value;
	}
	@Override
	public void renderPath(RenderContext rc) {
		
		String pp=rc.getPagePath();
		int p1=pp.lastIndexOf("/");
		int p2=pp.lastIndexOf(".");
		if(p2>p1){
			String p=pp.substring(p2+1);
			String ct=MimeMapping.mimes.get(p);
			if(ct!=null)
				rc.resp.addHeader(HttpHeaders.Names.CONTENT_TYPE, ct);
		}
		if(rc.req.param(Link.STAMP_STR)!=null){
			
		
			
			rc.setDateField(HttpHeaders.Names.EXPIRES,System.currentTimeMillis() +ONE_YEAR);
			rc.resp.addHeader(HttpHeaders.Names.CACHE_CONTROL, HttpHeaders.Values.MAX_AGE+"=315360000");
		}
		if(this.value!=null)
			rc.write(this.value);
		byte [] v=this.site.siteStore.getData(path, PathDataType.Res);
		rc.write(v);
		
		
	}
	 
	@Override
	public long lastModified(RenderContext rc) {
		// TODO Auto-generated method stub
		return lastModified;
	}
	 

}
